<script type="text/javascript" charset="utf-8" src="/public2/ueditor.config.js"></script>
<script type="text/javascript" charset="utf-8" src="/public2/ueditor.all.min.js"> </script>
<!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
<!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
<script type="text/javascript" charset="utf-8" src="/public2/lang/zh-cn/zh-cn.js"></script>
<div class="main">
    <!-- MAIN CONTENT -->
    <div class="main-content">
        <div class="container-fluid">
            <div class="row">
                <div class="col-md-12">
                    <div style="margin-bottom:10px;">
                        <span>封面图：</span>
                        <button type="button" id="file-label" class="btn btn-primary btn-sm">选择图片</button>
                        <input type="file" id="avatar" style="display:none"/>
                    </div>
                    <div id="img-container">
                        <img id="img" src="<%= data ? data.avatar : ''%>" alt="">
                    </div>
                    <div style="margin-bottom:10px;">
                        <span>标题：</span>
                        <input id="title" value="<%=data ? data.title : ''%>" />
                    </div>
                    <script type="text/plain" id="myEditor" style="width:1000px;height:240px;">
                                <%- data ? data.content : '' %>
                            </script>
                    <div class="input-container">
                        <button id="submit" class="btn btn-primary btn-block" style="width:1000px;margin-top:20px;">提交</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
    //实例化编辑器
    var file;
    var um = UE.getEditor('myEditor');
    $('#avatar').on('change', function (e) {
        if (e.target.files[0]) {
            var reader = new FileReader();
            reader.onloadend = function () {
                file = reader.result
                $('#img').attr('src', file);
            }
            reader.readAsDataURL(e.target.files[0]);
        }
    })
    $('#submit').on('click', function () {
        console.log(um.getContent());
        $.post('/admin/updateArticle', {
            content: um.getContent(),
            title: $('#title').val() || '',
            avatar: $('#img').attr('src') || '',
            id:'<%= id %>'
        }, function (data) {
            if (data.status === 0) {
                alert('保存成功');
                window.location.href = '/admin/consult'
            } else {
                alert(JSON.stringify(data.message));
            }
        })
    })
    $('#file-label').on('click', function () {
            $('#avatar').trigger('click');
        })
</script>